package com.zj.util;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class SysHttpUtil {


	public static String doHttpGet(String strUrl) {
		long startTime = System.currentTimeMillis();
		URL url = null;
		InputStream in = null;
		BufferedReader bufferedReader = null;
		StringBuilder res = new StringBuilder();
		HttpURLConnection httpurlconnection = null;
		try {
			url = new URL(strUrl);
			httpurlconnection = (HttpURLConnection) url.openConnection();
			httpurlconnection.setConnectTimeout(30000);
			httpurlconnection.setReadTimeout(30000);
			httpurlconnection.setRequestMethod("GET");
			int code = httpurlconnection.getResponseCode();
			if (code == 200) {
				in = httpurlconnection.getInputStream();
				bufferedReader = new BufferedReader(new InputStreamReader(in,
						"UTF-8"));
				String str = bufferedReader.readLine();
				while (str != null) {
					res.append(str);
					str = bufferedReader.readLine();
				}
			} else {
				System.out.println("================={}doHttpGet code is " + code);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (httpurlconnection != null) {
				httpurlconnection.disconnect();
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res.toString();
	}

	/**
	 * @param strUrl
	 * @return
	 */
	public static String doHttpPost(String strUrl) {
		long startTime = System.currentTimeMillis();
		URL url = null;
		InputStream in = null;
		BufferedReader bufferedReader = null;
		StringBuilder res = new StringBuilder();
		HttpURLConnection httpurlconnection = null;
		try {
			url = new URL(strUrl.substring(0, strUrl.indexOf("?")));
			httpurlconnection = (HttpURLConnection) url.openConnection();
			httpurlconnection.setDoOutput(true);
			httpurlconnection.setDoInput(true);
			httpurlconnection.setConnectTimeout(30000);
			httpurlconnection.setReadTimeout(30000);
			httpurlconnection.setUseCaches(false);
			httpurlconnection.setRequestMethod("POST");
			httpurlconnection.getOutputStream().write(
					strUrl.substring(strUrl.indexOf("?") + 1, strUrl.length())
							.getBytes("UTF-8"));
			httpurlconnection.getOutputStream().flush();
			httpurlconnection.getOutputStream().close();
			int code = httpurlconnection.getResponseCode();
			if (code == 200) {
				in = httpurlconnection.getInputStream();
				bufferedReader = new BufferedReader(new InputStreamReader(in,
						"UTF-8"));
				String str = bufferedReader.readLine();
				while (str != null) {
					res.append(str);
					str = bufferedReader.readLine();
				}
			}
		} catch (Exception e) {
			System.out.println("doHttpPost");
		} finally {
			if (httpurlconnection != null) {
				httpurlconnection.disconnect();
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					System.out.println("");
				}
			}
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					System.out.println("");
				}
			}
		}
		return res.toString();
	}



}
